/**
 * Copyright European Commission's
 * Taxation and Customs Union Directorate-General (DG TAXUD).
 */
package com.eryos;

import org.openqa.selenium.By;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eryos.selenium.SeleniumTestCase;

/**
 * 
 * @author $Author: $
 * @version $Revision: $
 */
public class FindCitiesByLocation extends SeleniumTestCase {

	static {
		System.setProperty("selenium.browser", "chrome");
		System.setProperty("selenium.server.host", "win-xp-1");
		System.setProperty("selenium.server.port", "4445");
	}

	private static final String FROM_FIELD = "pointa";
	private static final String TO_FIELD = "pointb";
	private static final String BUTTON = "show";
	private static final int LIMIT = 15;
	private static final int WAIT_TIME = 4000;

	/** The logger. */
	private static final Logger LOGGER = LoggerFactory
			.getLogger(FindCitiesByLocation.class);

	public void testCities() throws InterruptedException {

		try {

			// goTo("http://www.travelmath.com/flying-distance/from/Thionville,+France/to/Thionville,+France");
			goTo("http://www.freemaptools.com/how-far-is-it-between.htm");
			this.driver.findElements(By.name("dist")).get(1).click();
			this.typeValue(By.name(FROM_FIELD), "metz, 57, france");
			this.typeValue(By.name(TO_FIELD), "metz, 57, france");

			Thread.sleep(WAIT_TIME);
			this.click(By.name(BUTTON));
			Thread.sleep(WAIT_TIME);

			int cityId = 0;

			for (String ville : Constants.listOfAllCities) {

				this.typeValue(By.name(TO_FIELD), ville + ", 57, france");

				Thread.sleep(WAIT_TIME);

				this.click(By.name(BUTTON));

				Thread.sleep(WAIT_TIME);

				String value = this.driver.findElement(By.id("distance"))
						.getAttribute("value");
				String transport = this.driver.findElement(By.id("transport"))
						.getAttribute("value");
				Double val = 0.0;
				Double trans = 0.0;
				if (value != null && !value.isEmpty()
						&& !value.trim().equalsIgnoreCase("0")) {
					val = Double.parseDouble(value);
				}
				if (transport != null && !transport.isEmpty()
						&& !transport.trim().equalsIgnoreCase("0")) {
					trans = Double.parseDouble(transport);
				}
				System.out.println(ville + "\t" + val + "\t" + trans);
				// System.out.println("   -> <" + val + "> km");

				if (val > 0 && val <= (LIMIT + 1)) {
					System.out.println(ville);
					System.out.println("Metz - " + ville + " : <" + val
							+ "> km");
					System.out.println(ville + " added to the pool");
					// listOfCities.add(ville);

					cityId++;
				}

				Thread.sleep(WAIT_TIME / 2);
			}
			//
			// for (String city : listOfCities) {
			// System.out.println("\"" + city + "\",");
			// }

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Logger getLogger() {
		return LOGGER;
	}
}
